package topin.facecat.FaceCat.UI

import facecat.topin.core.FCColor
import facecat.topin.core.FCCursors
import facecat.topin.core.FCEventCallBack
import facecat.topin.core.FCEventID
import facecat.topin.core.FCFont
import facecat.topin.core.FCPaint
import facecat.topin.core.FCPoint
import facecat.topin.core.FCRect
import facecat.topin.core.FCSize
import facecat.topin.core.FCTouchInfo
import facecat.topin.core.FCTran
import facecat.topin.div.FCMenu
import facecat.topin.div.FCMenuItem
import facecat.topin.input.FCComboBox
import topin.facecat.FaceCat.UI.FCDraw.drawText
import topin.facecat.FaceCat.UI.FCDraw.drawUnderLineNum
import topin.facecat.FaceCat.Service.ADJUSTMENTFACTOR
import topin.facecat.FaceCat.Service.Security
import topin.facecat.FaceCat.Service.StockService
import topin.facecat.FaceCat.Service.StockService.dayOfWeek
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.GregorianCalendar


class FCChartEx : FCChart(), FCEventCallBack {
    var m_floatStrs = ArrayList<String>()
    var m_dates = ArrayList<Double>()
    override fun drawChartCrossLine(paint: FCPaint, clipRect: FCRect?) {
        if (m_datas == null || m_datas!!.size === 0) {
            return
        }
        val size = size
        val candleDivHeight = candleDivHeight
        val volDivHeight = volDivHeight
        val indDivHeight = indDivHeight
        var crossLineIndex = m_crossStopIndex
        if (crossLineIndex == -1 || !m_showCrossLine) {
            crossLineIndex = if (m_lastValidIndex !== -1) {
                m_lastValidIndex
            } else {
                m_lastVisibleIndex
            }
        }
        if (crossLineIndex < 0 || crossLineIndex > m_datas!!.size - 1) {
            return
        }
        if (volDivHeight > 0) {
            val drawTitles = ArrayList<String>()
            val drawColors = ArrayList<Long>()
            drawTitles.add(
                "VOL " +
                        FCTran.getValueByDigit(
                            m_datas!![crossLineIndex].m_volume / m_magnitude,
                            m_volDigit
                        )
            )
            drawColors.add(textColor)
            var iLeft = m_leftVScaleWidth + 5
            for (i in drawTitles.indices) {
                val tSize = paint.textSize(drawTitles[i], font, -1)
                paint.drawText(
                    drawTitles[i],
                    drawColors[i],
                    font,
                    FCRect(
                        iLeft,
                        candleDivHeight + 5 ,
                        iLeft + tSize.cx,
                        candleDivHeight + 5 + tSize.cy 
                    ),
                    -1
                )
                iLeft += tSize.cx + 5
            }
        }
        //上面显示数据  高开低收
        if (m_cycle.equals("trend")) {
            val drawTitles = ArrayList<String>()
            val drawColors = ArrayList<Long>()
            if (m_text.length > 0) {
                drawTitles.add(m_text)
                drawColors.add(textColor)
            }
            drawColors.add(textColor)
            var iLeft = m_leftVScaleWidth + 5
            for (i in drawTitles.indices) {
                val tSize = paint.textSize(drawTitles[i], font, -1)
                paint.drawText(
                    drawTitles[i], drawColors[i],
                    font, FCRect(iLeft, 5, iLeft + tSize.cx, 5 + tSize.cy), -1
                )
                iLeft += tSize.cx + 5
            }
        } else {
            val drawTitles = ArrayList<String>()
            val drawColors = ArrayList<Long>()
            if (m_text.length > 0) {
                drawTitles.add(m_text)
                drawColors.add(textColor)
            }
            if (m_mainIndicator.equals("MA")) {
                if (m_ma5.size > 0) {
                    drawTitles.add(
                        "MA5 " +
                                FCTran.getValueByDigit(m_ma5[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA5")
                }
                drawColors.add(m_indicatorColors[0])
                if (m_ma10.size > 0) {
                    drawTitles.add(
                        "MA10 " +
                                FCTran.getValueByDigit(m_ma10[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA10")
                }
                drawColors.add(m_indicatorColors[1])
                if (m_ma20.size > 0) {
                    drawTitles.add(
                        "MA20 " +
                                FCTran.getValueByDigit(m_ma20[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA20")
                }
                drawColors.add(m_indicatorColors[2])
                if (m_ma30.size > 0) {
                    drawTitles.add(
                        "MA30 " +
                                FCTran.getValueByDigit(m_ma30[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA30")
                }
                drawColors.add(m_indicatorColors[5])
                if (m_ma120.size > 0) {
                    drawTitles.add(
                        "MA120 " +
                                FCTran.getValueByDigit(m_ma120[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA120")
                }
                drawColors.add(m_indicatorColors[4])
                if (m_ma250.size > 0) {
                    drawTitles.add(
                        "MA250 " +
                                FCTran.getValueByDigit(m_ma250[crossLineIndex], m_candleDigit)
                    )
                } else {
                    drawTitles.add("MA250")
                }
                drawColors.add(m_indicatorColors[3])
            }
            var iLeft = m_leftVScaleWidth + 5
            for (i in drawTitles.indices) {
                val tSize = paint.textSize(drawTitles[i], font, -1)
                paint.drawText(
                    drawTitles[i], drawColors[i],
                    font, FCRect(iLeft, 5, iLeft + tSize.cx, 5 + tSize.cy), -1
                )
                if (i == 0 && m_text.length > 0) {
                    val midY = 13
                    val cSize = paint.textSize(m_subComboBox!!.text, m_subComboBox!!.font, -1)
                    val sWidth = cSize.cx + 25
                    val subBounds = FCRect(
                        iLeft + tSize.cx + 8,
                        midY - 10,
                        iLeft + tSize.cx + 8 + sWidth,
                        midY + 10
                    )
                    m_subComboBox!!.bounds = subBounds
                    iLeft += sWidth + 16
                }
                iLeft += tSize.cx + 5
            }
        }
        if (indDivHeight > 0) {
            val drawTitles = ArrayList<String>()
            val drawColors = ArrayList<Long>()
            if (m_showIndicator.equals("MACD")) {
                if (m_alldifarr.size > 0) {
                    drawTitles.add(
                        "DIF " +
                                FCTran.getValueByDigit(m_alldifarr[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("DIF")
                }
                drawColors.add(m_indicatorColors[0])
                if (m_alldeaarr.size > 0) {
                    drawTitles.add(
                        "DEA " +
                                FCTran.getValueByDigit(m_alldeaarr[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("DEA")
                }
                drawColors.add(m_indicatorColors[1])
                if (m_allmacdarr.size > 0) {
                    drawTitles.add(
                        "MACD " +
                                FCTran.getValueByDigit(m_allmacdarr[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("MACD")
                }
                drawColors.add(m_indicatorColors[4])
            } else if (m_showIndicator.equals("KDJ")) {
                if (m_kdjK.size > 0) {
                    drawTitles.add(
                        "K " +
                                FCTran.getValueByDigit(m_kdjK[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("K")
                }
                drawColors.add(m_indicatorColors[0])
                if (m_kdjD.size > 0) {
                    drawTitles.add(
                        "D " +
                                FCTran.getValueByDigit(m_kdjD[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("D")
                }
                drawColors.add(m_indicatorColors[1])
                if (m_kdjJ.size > 0) {
                    drawTitles.add(
                        "J " +
                                FCTran.getValueByDigit(m_kdjJ[crossLineIndex], m_indDigit)
                    )
                } else {
                    drawTitles.add("J")
                }
                drawColors.add(m_indicatorColors[2])
            }
            var iLeft = m_leftVScaleWidth + 5
            for (i in drawTitles.indices) {
                val tSize = paint.textSize(drawTitles[i], font, -1)
                paint.drawText(
                    drawTitles[i], drawColors[i], font, FCRect(
                        iLeft,
                        candleDivHeight + volDivHeight + 5 , iLeft + tSize.cx,
                        candleDivHeight + volDivHeight + 5 + tSize.cy 
                    ), -1
                )
                iLeft += tSize.cx + 5
            }
        }
        if (m_showCrossLine) {
            var rightText: String? = ""
            if (m_touchPosition.y < candleDivHeight) {
                rightText = FCTran.getValueByDigit(
                    getChartValue(m_touchPosition), m_candleDigit
                )
            } else if (m_touchPosition.y > candleDivHeight &&
                m_touchPosition.y < candleDivHeight + volDivHeight
            ) {
                rightText = FCTran.getValueByDigit(
                    getChartValue(m_touchPosition) / m_magnitude, m_volDigit
                )
            } else if (m_touchPosition.y > candleDivHeight + volDivHeight &&
                m_touchPosition.y < candleDivHeight + volDivHeight + indDivHeight
            ) {
                rightText = FCTran.getValueByDigit(
                    getChartValue(m_touchPosition), m_indDigit
                )
            }
            var drawY = m_touchPosition.y
            if (drawY > size.cy) {
                drawY = size.cy
            }
            val tSize2 = paint.textSize(rightText, font, -1)
            if (m_leftVScaleWidth > 0) {
                paint.fillRect(
                    m_crossTipColor, m_leftVScaleWidth - tSize2.cx, drawY - tSize2.cy / 2 - 4,
                    m_leftVScaleWidth, drawY + tSize2.cy / 2 + 3
                )
                paint.drawText(
                    rightText,
                    textColor,
                    font,
                    FCRect(
                        m_leftVScaleWidth - tSize2.cx,
                        drawY - tSize2.cy / 2,
                        m_leftVScaleWidth,
                        drawY + tSize2.cy / 2
                    ), -1
                )
            }
            if (m_rightVScaleWidth > 0) {
                paint.fillRect(
                    m_crossTipColor, size.cx - m_rightVScaleWidth, drawY - tSize2.cy / 2 - 4,
                    size.cx - m_rightVScaleWidth + tSize2.cx, drawY + tSize2.cy / 2 + 3
                )
                paint.drawText(
                    rightText,
                    textColor,
                    font,
                    FCRect(
                        size.cx - m_rightVScaleWidth,
                        drawY - tSize2.cy / 2,
                        size.cx - m_rightVScaleWidth + tSize2.cx,
                        drawY + tSize2.cy / 2
                    ), -1
                )
            }
            //绘制十字线
            var drawX = getChartX(m_crossStopIndex)
            if (m_targetOldX === 0f && m_targetOldX2 === 0f) {
                drawX = m_touchPosition.x
            }
            if (drawX < m_leftVScaleWidth) {
                drawX = m_leftVScaleWidth
            }
            if (drawX > size.cx - m_rightVScaleWidth) {
                drawX = size.cx - m_rightVScaleWidth
            }
            if (m_showCrossLine) {
                paint.drawLine(
                    m_crossLineColor, m_lineWidthChart, 0,
                    m_leftVScaleWidth, drawY, size.cx - m_rightVScaleWidth, drawY
                )
                paint.drawLine(
                    m_crossLineColor, m_lineWidthChart, 0, drawX, 0,
                    drawX, size.cy - m_hScaleHeight
                )
            }
            val cRealIndex = getChartIndexReal(touchPoint)
            if (cRealIndex != -1 && cRealIndex > m_firstVisibleIndex && (cRealIndex < m_datas!!.size || cRealIndex - m_firstVisibleIndex < m_dates.size)) {
                var dateNum = 0.0
                dateNum = if (m_dates.size > 0 && cRealIndex - m_firstVisibleIndex < m_dates.size) {
                    m_dates[cRealIndex - m_firstVisibleIndex]
                } else {
                    m_datas!![cRealIndex].m_date
                }
                var xText = dateNum.toString()
                val dt = FCTran.numToDate(dateNum)
                if (m_cycle.equals("day")) {
                    val format = SimpleDateFormat("yyyy/MM/dd")
                    xText = format.format(dt.time) + ","
                    xText += getWeekStr(
                        dayOfWeek(
                            dt[Calendar.YEAR], dt[Calendar.MONTH] + 1,
                            dt[Calendar.DAY_OF_MONTH]
                        )
                    )
                } else if (m_cycle.equals("minute")) {
                    val format = SimpleDateFormat("yyyy/MM/dd HH:mm")
                    xText = format.format(dt.time) + ","
                    xText += getWeekStr(
                        dayOfWeek(
                            dt[Calendar.YEAR], dt[Calendar.MONTH] + 1,
                            dt[Calendar.DAY_OF_MONTH]
                        )
                    )
                } else {
                    xText = if (m_hScaleFormat.length > 0) {
                        val format = SimpleDateFormat(m_hScaleFormat)
                        format.format(dt.time)
                    } else {
                        val format = SimpleDateFormat("HH:mm")
                        format.format(dt.time)
                    }
                }
                val xSize = paint.textSize(xText, font, -1)
                val bTop = height - m_hScaleHeight
                paint.fillRect(
                    m_crossTipColor,
                    drawX - xSize.cx / 2 - 2,
                    bTop,
                    drawX + xSize.cx / 2 + 2,
                    bTop + xSize.cy + 6
                )
                paint.drawText(
                    xText, textColor, font, FCRect(
                        drawX - xSize.cx / 2,
                        bTop + 3, drawX + xSize.cx / 2,
                        bTop + 3 + xSize.cy
                    ), -1
                )
            }
        }
    }

    var m_touchFactor: ADJUSTMENTFACTOR? = null
    var m_sIndex = -1
    override fun drawChartScale(paint: FCPaint, clipRect: FCRect?) {
        m_dates.clear()
        m_touchFactor = null
        m_sIndex = -1
        val size = size
        val candleDivHeight = candleDivHeight
        val volDivHeight = volDivHeight
        val indDivHeight = indDivHeight
        if (m_leftVScaleWidth > 0) {
            paint.drawLine(
                m_scaleColor, m_lineWidthChart, 0,
                m_leftVScaleWidth, 0, m_leftVScaleWidth,
                size.cy
            )
        }
        if (m_rightVScaleWidth > 0) {
            paint.drawLine(
                m_scaleColor, m_lineWidthChart, 0,
                size.cx - m_rightVScaleWidth, 0, size.cx - m_rightVScaleWidth,
                size.cy
            )
        }
        if (m_hScaleHeight > 0) {
            paint.drawLine(
                m_scaleColor, m_lineWidthChart, 0, 0,
                height - m_hScaleHeight, size.cx, height - m_hScaleHeight
            )
        }
        if (volDivHeight > 0) {
            paint.drawLine(
                m_scaleColor, m_lineWidthChart, 0,
                0, candleDivHeight, size.cx, candleDivHeight
            )
        }
        if (indDivHeight > 0) {
            paint.drawLine(
                m_scaleColor, m_lineWidthChart, 0,
                m_leftVScaleWidth, candleDivHeight + volDivHeight ,
                size.cx - m_rightVScaleWidth,
                candleDivHeight + volDivHeight 
            )
        }
        if (m_datas!!.size > 0) {
            var topPoint = FCPoint(0, 20)
            var bottomPoint = FCPoint(0, candleDivHeight - 10)
            val candleMax = getChartValue(topPoint)
            val candleMin = getChartValue(bottomPoint)
            chartGridScale(
                candleMin,
                candleMax,
                (candleDivHeight - m_candlePaddingTop - m_candlePaddingBottom) / 2,
                m_vScaleDistance.toDouble(),
                (m_vScaleDistance / 2).toDouble(),
                (candleDivHeight - m_candlePaddingTop - m_candlePaddingBottom) / m_vScaleDistance
            )
            if (m_gridStep > 0) {
                val drawValues = ArrayList<Double>()
                val isTrend = m_cycle.equals("trend")
                var firstOpen = 0.0
                if (isTrend) {
                    firstOpen = if (m_firstOpen !== 0.0) {
                        m_firstOpen
                    } else {
                        m_datas!![m_firstVisibleIndex].m_close
                    }
                    if (firstOpen == 0.0) {
                        firstOpen = candleMin
                    }
                    var subValue = candleMax - candleMin
                    val count = (candleDivHeight -
                            m_candlePaddingTop -
                            m_candlePaddingBottom) /
                            m_vScaleDistance
                    if (count > 0) {
                        subValue /= count.toDouble()
                    }
                    var start = firstOpen
                    while (start < candleMax) {
                        start += subValue
                        if (start <= candleMax) {
                            drawValues.add(start)
                        }
                    }
                    start = firstOpen
                    while (start > candleMin) {
                        start -= subValue
                        if (start >= candleMin) {
                            drawValues.add(start)
                        }
                    }
                } else {
                    var start = 0.0
                    if (candleMin >= 0) {
                        while (start + m_gridStep < candleMin) {
                            start += m_gridStep
                        }
                    } else {
                        while (start - m_gridStep > candleMin) {
                            start -= m_gridStep
                        }
                    }
                    while (start <= candleMax) {
                        if (start > candleMin) {
                            drawValues.add(start)
                        }
                        start += m_gridStep
                    }
                }
                drawValues.add(firstOpen)
                for (i in drawValues.indices) {
                    val start = drawValues[i]
                    val hAxisY = getChartY(0, start)
                    if (hAxisY < 1 || hAxisY >= candleDivHeight) {
                        continue
                    }
                    if (m_cycle.equals("trend") && start == firstOpen) {
                        paint.drawLine(
                            m_scaleColor, 1f, 0,
                            m_leftVScaleWidth, hAxisY,
                            size.cx - m_rightVScaleWidth, hAxisY
                        )
                    } else {
                        paint.drawLine(
                            m_gridColor, m_lineWidthChart, 0,
                            m_leftVScaleWidth, hAxisY,
                            size.cx - m_rightVScaleWidth, hAxisY
                        )
                    }
                    paint.drawLine(
                        m_scaleColor, m_lineWidthChart, 0,
                        m_leftVScaleWidth - 8, hAxisY, m_leftVScaleWidth, hAxisY
                    )
                    paint.drawLine(
                        m_scaleColor, m_lineWidthChart, 0,
                        size.cx - m_rightVScaleWidth, hAxisY,
                        size.cx - m_rightVScaleWidth + 8, hAxisY
                    )
                    val drawText = FCTran.getValueByDigit(start, m_candleDigit)
                    val tSize = paint.textSize(drawText, font, -1)
                    if (isTrend) {
                        val diffRange = (start - firstOpen) / firstOpen * 100
                        val diffRangeStr = FCTran.getValueByDigit(diffRange, 2) + "%"
                        val dSize = paint.textSize(diffRangeStr, font, -1)
                        if (diffRange > 0) {
                            paint.drawText(
                                diffRangeStr, FCColor.rgb(255, 82, 82), font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + dSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            drawUnderLineNum(
                                paint,
                                start,
                                m_candleDigit,
                                font,
                                FCColor.rgb(255, 82, 82),
                                false,
                                m_leftVScaleWidth - tSize.cx - 10,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        } else if (diffRange < 0) {
                            paint.drawText(
                                diffRangeStr, FCColor.rgb(46, 255, 50), font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + dSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            drawUnderLineNum(
                                paint,
                                start,
                                m_candleDigit,
                                font,
                                FCColor.rgb(46, 255, 50),
                                false,
                                m_leftVScaleWidth - tSize.cx - 10,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        } else {
                            paint.drawText(
                                diffRangeStr, m_textColor, font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + dSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            drawUnderLineNum(
                                paint,
                                start,
                                m_candleDigit,
                                font,
                                m_textColor,
                                false,
                                m_leftVScaleWidth - tSize.cx - 10,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        }
                    } else {
                        var digit = m_candleDigit
                        if (m_candleMax > 1000) {
                            digit = 0
                        }
                        if (MyColor.m_style === 0) {
                            drawUnderLineNum(
                                paint,
                                start,
                                digit,
                                font,
                                FCColor.rgb(233, 153, 46),
                                false,
                                size.cx - m_rightVScaleWidth + 12,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        } else {
                            drawUnderLineNum(
                                paint, start, digit,
                                font, textColor, false, size.cx - m_rightVScaleWidth + 12,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        }
                        if (MyColor.m_style === 0) {
                            drawUnderLineNum(
                                paint,
                                start,
                                digit,
                                font,
                                FCColor.rgb(233, 153, 46),
                                false,
                                m_leftVScaleWidth - tSize.cx - 10,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        } else {
                            drawUnderLineNum(
                                paint,
                                start,
                                digit,
                                font,
                                textColor,
                                false,
                                m_leftVScaleWidth - tSize.cx - 10,
                                hAxisY - tSize.cy / 2 - 1
                            )
                        }
                    }
                }
            }
            topPoint = FCPoint(0, candleDivHeight  + 10)
            bottomPoint = FCPoint(0, candleDivHeight + volDivHeight  - 10)
            val volMax = getChartValue(topPoint)
            val volMin = getChartValue(bottomPoint)
            chartGridScale(
                volMin, volMax, (volDivHeight - m_volPaddingTop - m_volPaddingBottom) / 2,
                m_vScaleDistance.toDouble(), (m_vScaleDistance / 2).toDouble(),
                (volDivHeight - m_volPaddingTop - m_volPaddingBottom) / m_vScaleDistance
            )
            if (m_gridStep > 0) {
                var start = 0.0
                if (volMin >= 0) {
                    while (start + m_gridStep < volMin) {
                        start += m_gridStep
                    }
                } else {
                    while (start - m_gridStep > volMin) {
                        start -= m_gridStep
                    }
                }
                while (start <= volMax) {
                    if (start > volMin) {
                        val hAxisY = getChartY(1, start)
                        if (hAxisY < candleDivHeight  ||
                            hAxisY > candleDivHeight + volDivHeight 
                        ) {
                            start += m_gridStep
                            continue
                        }
                        paint.drawLine(
                            m_gridColor, m_lineWidthChart, 0, m_leftVScaleWidth,
                            hAxisY, size.cx - m_rightVScaleWidth, hAxisY
                        )
                        paint.drawLine(
                            m_scaleColor, m_lineWidthChart, 0,
                            m_leftVScaleWidth - 8, hAxisY, m_leftVScaleWidth, hAxisY
                        )
                        paint.drawLine(
                            m_scaleColor, m_lineWidthChart, 0, size.cx - m_rightVScaleWidth,
                            hAxisY, size.cx - m_rightVScaleWidth + 8, hAxisY
                        )
                        val drawText = FCTran.getValueByDigit(start / m_magnitude, m_volDigit)
                        val tSize = paint.textSize(drawText, font, -1)
                        if (MyColor.m_style === 0) {
                            paint.drawText(
                                drawText, FCColor.rgb(233, 153, 46), font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + tSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            paint.drawText(
                                drawText, FCColor.rgb(233, 153, 46),
                                font,
                                FCRect(
                                    m_leftVScaleWidth - tSize.cx - 10,
                                    hAxisY - tSize.cy / 2,
                                    m_leftVScaleWidth - 10,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                        } else {
                            paint.drawText(
                                drawText, textColor, font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + tSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            paint.drawText(
                                drawText,
                                textColor,
                                font,
                                FCRect(
                                    m_leftVScaleWidth - tSize.cx - 10,
                                    hAxisY - tSize.cy / 2,
                                    m_leftVScaleWidth - 10,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                        }
                    }
                    start += m_gridStep
                }
            }
            if (indDivHeight > 0) {
                topPoint = FCPoint(0, candleDivHeight + volDivHeight  + 10)
                bottomPoint =
                    FCPoint(0, candleDivHeight + volDivHeight + indDivHeight  - 10)
                val indMax = getChartValue(topPoint)
                val indMin = getChartValue(bottomPoint)
                chartGridScale(
                    indMin, indMax, (indDivHeight - m_indPaddingTop - m_indPaddingBottom) / 2,
                    m_vScaleDistance.toDouble(), (m_vScaleDistance / 2).toDouble(),
                    (indDivHeight - m_indPaddingTop - m_indPaddingBottom) / m_vScaleDistance
                )
                if (m_gridStep > 0) {
                    var start = 0.0
                    if (indMin >= 0) {
                        while (start + m_gridStep < indMin) {
                            start += m_gridStep
                        }
                    } else {
                        while (start - m_gridStep > indMin) {
                            start -= m_gridStep
                        }
                    }
                    while (start <= indMax) {
                        if (start > indMin) {
                            val hAxisY = getChartY(2, start)
                            if (hAxisY < candleDivHeight + volDivHeight ||
                                hAxisY > candleDivHeight + volDivHeight + indDivHeight
                            ) {
                                start += m_gridStep
                                continue
                            }
                            paint.drawLine(
                                m_gridColor,
                                m_lineWidthChart,
                                0,
                                m_leftVScaleWidth,
                                hAxisY,
                                size.cx - m_rightVScaleWidth,
                                hAxisY
                            )
                            paint.drawLine(
                                m_scaleColor,
                                m_lineWidthChart,
                                0,
                                m_leftVScaleWidth - 8,
                                hAxisY,
                                m_leftVScaleWidth,
                                hAxisY
                            )
                            paint.drawLine(
                                m_scaleColor,
                                m_lineWidthChart,
                                0,
                                size.cx - m_rightVScaleWidth,
                                hAxisY,
                                size.cx - m_rightVScaleWidth + 8,
                                hAxisY
                            )
                            val drawText = FCTran.getValueByDigit(start, m_indDigit)
                            val tSize = paint.textSize(drawText, font, -1)
                            paint.drawText(
                                drawText, textColor, font,
                                FCRect(
                                    size.cx - m_rightVScaleWidth + 10,
                                    hAxisY - tSize.cy / 2,
                                    size.cx - m_rightVScaleWidth + 10 + tSize.cx,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                            paint.drawText(
                                drawText,
                                textColor,
                                font,
                                FCRect(
                                    m_leftVScaleWidth - tSize.cx - 10,
                                    hAxisY - tSize.cy / 2,
                                    m_leftVScaleWidth - 10,
                                    hAxisY + tSize.cy / 2
                                ), -1
                            )
                        }
                        start += m_gridStep
                    }
                }
            }
        }
        if (m_datas!!.size > 0 && m_hScaleHeight > 0) {
            var newScaleColor = textColor
            if (MyColor.m_style === 0) {
                newScaleColor = FCColor.rgb(233, 153, 46)
            }
            if (m_cycle.equals("trend")) {
                val times = ArrayList<Double>()
                if (width < 600) {
                    times.add(FCTran.getDateNum(1970, 1, 1, 10, 30, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 11, 30, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 14, 0, 0, 0))
                } else {
                    times.add(FCTran.getDateNum(1970, 1, 1, 10, 0, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 10, 30, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 11, 0, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 11, 30, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 13, 30, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 14, 0, 0, 0))
                    times.add(FCTran.getDateNum(1970, 1, 1, 14, 30, 0, 0))
                }
                val bBottom = height
                val leftStr = "9:30"
                val leftSize = paint.textSize(leftStr, font, -1)
                val rightStr = "15:00"
                val rightSize = paint.textSize(rightStr, font, -1)
                paint.drawText(
                    leftStr,
                    newScaleColor,
                    font,
                    FCRect(
                        m_leftVScaleWidth + 5,
                        bBottom - (m_hScaleHeight + leftSize.cy) / 2,
                        m_leftVScaleWidth + leftSize.cx + 5,
                        bBottom - (m_hScaleHeight + leftSize.cy) / 2
                    ),
                    -1
                )
                paint.drawText(
                    rightStr,
                    newScaleColor,
                    font,
                    FCRect(
                        size.cx - m_rightVScaleWidth - rightSize.cx - 5,
                        bBottom - (m_hScaleHeight + leftSize.cy) / 2,
                        size.cx - m_rightVScaleWidth - 5,
                        bBottom - (m_hScaleHeight + leftSize.cy) / 2
                    ),
                    -1
                )
                for (i in m_firstVisibleIndex..m_lastVisibleIndex) {
                    val dateNum = m_datas!![i].m_date
                    val dt = FCTran.numToDate(dateNum)
                    val hour = dt[Calendar.HOUR_OF_DAY]
                    val minute = dt[Calendar.MINUTE]
                    val timeNum = FCTran.getDateNum(
                        1970, 1, 1,
                        dt[Calendar.HOUR_OF_DAY], dt[Calendar.MINUTE], 0, 0
                    )
                    if (times.contains(timeNum)) {
                        val format = SimpleDateFormat("HH:mm")
                        val xText = format.format(dt.time)
                        val tSize = paint.textSize(xText, font, -1)
                        val x = getChartX(i)
                        val dx = x - tSize.cx / 2
                        paint.drawLine(
                            m_scaleColor,
                            m_lineWidthChart,
                            0,
                            x,
                            bBottom - m_hScaleHeight,
                            x,
                            bBottom - m_hScaleHeight + 5
                        )
                        paint.drawText(
                            xText,
                            newScaleColor,
                            font,
                            FCRect(
                                dx,
                                bBottom - (m_hScaleHeight + leftSize.cy) / 2,
                                dx + tSize.cx,
                                bBottom - (m_hScaleHeight + leftSize.cy) / 2
                            ),
                            -1
                        )
                        paint.drawLine(m_gridColor, 1f, 0, x, 0, x, bBottom - m_hScaleHeight)
                    }
                }
            } else {
                val dLeft = m_leftVScaleWidth.toDouble()
                if (m_cycle.equals("day")) {
                    if (StockService.m_factors.containsKey(m_myChart!!.m_code)) {
                        val factor = StockService.m_factors[m_myChart!!.m_code]!!
                        var startIndex = m_firstVisibleIndex
                        val txtSignal = "S"
                        val sFont = FCFont("Default", 10.5f, false, false, false)
                        val sSize = paint.textSize(txtSignal, sFont, -1)
                        val mp = touchPoint
                        for (i in factor.indices) {
                            val date = factor[i].dwDate
                            var drawIndex = -1
                            for (j in startIndex..m_lastVisibleIndex) {
                                var lastDate = 0.0
                                var nextDate = 0.0
                                if (j > 0) {
                                    lastDate = m_datas!![j - 1].m_date
                                }
                                if (j < m_datas!!.size - 1) {
                                    nextDate = m_datas!![j + 1].m_date
                                }
                                if (date == m_datas!![j].m_date || date > lastDate && date < nextDate) {
                                    drawIndex = j
                                    startIndex = j + 1
                                    break
                                }
                            }
                            if (drawIndex != -1) {
                                val x = getChartX(drawIndex)
                                drawText(
                                    paint,
                                    txtSignal,
                                    FCColor.rgb(255, 0, 0),
                                    sFont,
                                    x - sSize.cx / 2,
                                    candleDivHeight - sSize.cy
                                )
                                if (mp.x >= x - sSize.cx / 2 && mp.x <= x + sSize.cx / 2 && mp.y >= candleDivHeight - sSize.cy && mp.y <= candleDivHeight) {
                                    m_touchFactor = factor[i]
                                    m_sIndex = drawIndex
                                }
                            }
                        }
                    }
                }
                if (m_cycle.equals("day")) {
                    val fIndex = m_firstVisibleIndex
                    val lIndex = m_lastVisibleIndex
                    if (lIndex >= fIndex) {
                        val dates = ArrayList<Double>()
                        for (d in fIndex..lIndex) {
                            dates.add(m_datas!![d].m_date)
                        }
                        val drawLeft = m_leftVScaleWidth //左侧起画点
                        var i = fIndex //开始索引
                        var lastYear = 0 //缓存年份，用于判断是否换年
                        val drawYearsCache = ArrayList<Int>() //实际绘制到图形上的年份文字
                        var lastTextRight = 0 //上个文字的右侧
                        val timeCache = ArrayList<Calendar>() //保存日期的缓存
                        val yearTextLeftCache = ArrayList<Int>() //绘制年文字的左侧位置缓存
                        val yearTextRightCache = ArrayList<Int>() //绘制年文字的右侧位置缓存
                        val textPadding = 5 //两个文字之间的最小间隔
                        //逐步递增索引，先绘制年
                        val bBottom = height
                        while (i <= lIndex) {
                            val dateObj = FCTran.numToDate(dates[i - fIndex])
                            timeCache.add(dateObj)
                            val year = dateObj[Calendar.YEAR] //从结构中获取年份
                            val x = getChartX(i) //获取索引对应的位置
                            //                            判断是否换年，以及是否在绘图区间内
                            if (year != lastYear && x >= drawLeft && x < size.cx - m_rightVScaleWidth) {
                                val month = dateObj[Calendar.MONTH] + 1 //获取月的结构
                                val xText = year.toString() //拼接要绘制的文字
                                val tSize = paint.textSize(xText, font, -1) //计算要绘制文字的大小
                                //判断是否和上个文字重影
                                val tLeft = x + 2
                                if (tLeft > lastTextRight) {
                                    paint.drawLine(
                                        m_scaleColor,
                                        1f,
                                        0,
                                        x,
                                        bBottom - m_hScaleHeight,
                                        x,
                                        bBottom
                                    ) //绘制刻度线
                                    drawText(
                                        paint, xText, newScaleColor,
                                        font, tLeft, bBottom - (m_hScaleHeight + tSize.cy) / 2
                                    ) //绘制文字
                                    yearTextLeftCache.add(tLeft) //将年文字的左侧位置缓存
                                    yearTextRightCache.add(tLeft + tSize.cx + 5) //将年文字的右侧位置缓存
                                    drawYearsCache.add(year) //缓存要绘制的年
                                    lastTextRight = tLeft + tSize.cx + 5 //缓存上个文字的右侧位置
                                    lastYear = year //记录上次绘制的年份
                                }
                            }
                            i = i + 1 //索引累加
                        }
                        if (m_myChart!!.m_cycle <= 1440) {
                            //绘制月份
                            for (m in drawYearsCache.indices) {
                                val cacheYear = drawYearsCache[m] //从缓存中获取年份
                                var lastMonth = 0 //缓存月份，用于判断是否换月
                                i = m_firstVisibleIndex //重置开始索引
                                lastTextRight = 0 //重置上个文字的右侧
                                //逐步递增索引
                                while (i <= lIndex) {
                                    val dateObj = timeCache[i - m_firstVisibleIndex] //从缓存中获取time
                                    val year = dateObj[Calendar.YEAR] //从结构中获取年份
                                    //判断是否同一年
                                    if (cacheYear == year) {
                                        val month = dateObj[Calendar.MONTH] + 1 //从结构中获取月份
                                        val x = getChartX(i)
                                        //判断是否换月，以及是否在绘图区间内
                                        if (lastMonth != month && x >= drawLeft && x < size.cx - m_rightVScaleWidth) {
                                            var xText = month.toString() //获取绘制的月份文字
                                            if (xText.length == 1) {
                                                xText = "0$xText"
                                            }
                                            val tSize = paint.textSize(xText, font, -1) //计算要绘制文字的大小
                                            //判断是否和上个文字重影
                                            if (x > lastTextRight + textPadding) {
                                                if (x > yearTextRightCache[m] + textPadding
                                                    && (m == drawYearsCache.size - 1 || m < drawYearsCache.size - 1 && x + tSize.cx < yearTextLeftCache[m + 1] - textPadding)
                                                ) {
                                                    paint.drawLine(
                                                        m_scaleColor,
                                                        1f,
                                                        0,
                                                        x,
                                                        bBottom - m_hScaleHeight,
                                                        x,
                                                        bBottom - m_hScaleHeight + 12
                                                    ) //绘制刻度
                                                    drawText(
                                                        paint,
                                                        xText,
                                                        newScaleColor,
                                                        font,
                                                        x + 2,
                                                        bBottom - (m_hScaleHeight + tSize.cy) / 2
                                                    ) //绘制文字
                                                    lastTextRight = x + tSize.cx + 2 //缓存上个文字的右侧位置
                                                }
                                            }
                                            lastMonth = month //记录上次绘制的月份
                                        }
                                    } else if (cacheYear < year) {
                                        break //超过区间，退出循环
                                    }
                                    i = i + 1 //索引累加
                                }
                            }
                        }
                    }
                } else {
                    val fIndex = m_firstVisibleIndex
                    val lIndex = m_lastVisibleIndex
                    if (lIndex >= fIndex) {
                        var lastDate2 = FCTran.numToDate(0.0)
                        val dates = ArrayList<Double>()
                        for (d in fIndex..lIndex) {
                            dates.add(m_datas!![d].m_date)
                        }
                        var i = fIndex
                        while (i <= lIndex) {
                            val dateNum = dates[i - fIndex]
                            var xText = dateNum.toString()
                            val dt = FCTran.numToDate(dateNum)
                            val dText: Calendar = GregorianCalendar(
                                dt[Calendar.YEAR],
                                dt[Calendar.MONTH + 1], dt[Calendar.DAY_OF_MONTH]
                            )
                            xText = if (dText[Calendar.YEAR] != lastDate2[Calendar.YEAR]) {
                                val format = SimpleDateFormat("HH:mm")
                                format.format(dt.time)
                                //                                xText = dt.toString("yyyy/MM/dd");
                            } else {
                                val format = SimpleDateFormat("HH:mm")
                                format.format(dt.time)
                                //                                xText = dt.toString("MM/dd");
                            }
                            if (dText != lastDate2) {
                                lastDate2 = dText
                                val tSize = paint.textSize(xText, font, -1)
                                val x = getChartX(i)
                                val dx = x + 2
                                if (dx > dLeft && dx + tSize.cx < size.cx - m_rightVScaleWidth - 5) {
                                    val bBottom = height
                                    paint.drawLine(
                                        m_scaleColor,
                                        m_lineWidthChart,
                                        0,
                                        x,
                                        bBottom - m_hScaleHeight,
                                        x,
                                        bBottom - m_hScaleHeight + 12
                                    )
                                    paint.drawText(
                                        xText,
                                        newScaleColor,
                                        font,
                                        FCRect(
                                            dx,
                                            bBottom - (m_hScaleHeight + tSize.cy) / 2,
                                            dx + tSize.cx,
                                            bBottom - (m_hScaleHeight + tSize.cy) / 2
                                        ),
                                        -1
                                    )
                                    i =
                                        i + ((tSize.cx + m_hScaleTextDistance) / m_hScalePixel).toInt() + 1
                                }
                            }
                            i++
                        }
                    }
                }
            }
        }
    }

    fun getChartIndexReal(mp: FCPoint): Int {
        if (m_datas!!.size === 0) {
            return -1
        }
        if (mp.x <= 0) {
            return 0
        }
        var intX = mp.x - m_leftVScaleWidth - m_hScalePixel - m_offsetX
        if (intX < 0) {
            intX = 0.0
        }
        var index = (m_firstVisibleIndex + intX / m_hScalePixel).toInt()
        val intPixel = m_hScalePixel.toInt()
        if (intPixel > 0 && intX % intPixel != 0.0) {
            index++
        }
        return index
    }

    override fun onPaint(paint: FCPaint, clipRect: FCRect) {
        super.onPaint(paint, clipRect!!)
        val cycle = m_myChart!!.m_cycle
        var sName = ""
        if (cycle == 0) {
            sName = "实时"
        } else if (cycle < 1440) {
            sName = FCTran.intToStr(cycle) + "分"
        } else if (cycle == 1440) {
            sName = "日线"
        } else if (cycle == 10080) {
            sName = "周线"
        } else if (cycle == 43200) {
            sName = "月线"
        } else if (cycle == 129600) {
            sName = "季线"
        } else if (cycle == 259200) {
            sName = "半年线"
        } else if (cycle == 518400) {
            sName = "年线"
        }
        if (sName.length > 0) {
            val font = font
            val tSize = paint.textSize(sName, font, -1)
            val width = width
            val height = height
            val tX = width - m_rightVScaleWidth / 2 - tSize.cx / 2
            val tY = getHeight() - m_hScaleHeight / 2 - tSize.cy / 2
            if (MyColor.m_style === 0) {
                drawText(paint, sName, FCColor.rgb(249, 237, 6), font, tX, tY)
            } else {
                drawText(paint, sName, FCColor.Text, font, tX, tY)
            }
        }
        if (m_touchFactor != null) {
            val x = getChartX(m_sIndex)
            val candleDivHeight = candleDivHeight
            val strs = ArrayList<String>()
            strs.add("权息变动")
            val format = SimpleDateFormat("yyyy/MM/dd")
            val xText = format.format(FCTran.numToDate(m_touchFactor!!.dwDate).time)
            strs.add("除权除息:$xText")
            if (m_touchFactor!!.f1 > 0) {
                strs.add("每10股派现金" + FCTran.floatToStr(m_touchFactor!!.f1) + "元")
            }
            if (m_touchFactor!!.f3 > 0) {
                strs.add("每10股送股" + FCTran.floatToStr(m_touchFactor!!.f1) + "股")
            }
            if (m_touchFactor!!.f4 > 0) {
                strs.add("每10股配股" + FCTran.floatToStr(m_touchFactor!!.f1) + "元")
            }
            val sRect = FCRect(x, candleDivHeight, x + 150, candleDivHeight + strs.size * 20)
            if (sRect.bottom > height) {
                val sHeight = sRect.bottom - sRect.top
                sRect.top = candleDivHeight - sHeight - 20
                sRect.bottom = sRect.top + sHeight
            }
            paint.fillRect(FCColor.Back, sRect)
            paint.drawRect(FCColor.Border, 1f, 0, sRect)
            for (i in strs.indices) {
                val str = strs[i]
                drawText(paint, str, textColor, font, sRect.left + 3, sRect.top + 2 + i * 20)
            }
        }
        if (m_floatStrs.size > 0 && m_tick == 0) {
            val rowHeight = 18
            val font = FCFont("Default", 13f, false, false, false)
            val mp = touchPoint
            var tWidth = 0
            for (i in m_floatStrs.indices) {
                val str = m_floatStrs[i]
                val tSize = paint.textSize(str, font, -1)
                if (tWidth < tSize.cx) {
                    tWidth = tSize.cx
                }
            }
            val sRect = FCRect(
                mp.x + 15,
                mp.y,
                mp.x + tWidth + 30,
                mp.y + m_floatStrs.size * rowHeight + 10
            )
            val sWidth = sRect.right - sRect.left
            val sHeight = sRect.bottom - sRect.top
            if (sRect.bottom > size.cy) {
                sRect.top = 20
                sRect.bottom = sRect.top + sHeight
            } else if (sRect.top < 0) {
                sRect.top = 0
                sRect.bottom = sHeight
            }
            if (sRect.right > width) {
                sRect.left = width - sWidth
                sRect.right = sRect.left + sWidth
            }
            if (MyColor.m_style === 0) {
                m_native.paint.fillRect(FCColor.rgba(0, 0, 0, 150), sRect)
            } else {
                m_native.paint.fillRect(FCColor.rgba(255, 255, 255, 150), sRect)
            }
            m_native.paint.drawRect(MyColor.USERCOLOR104, 1f, 0, sRect)
            for (i in m_floatStrs.indices) {
                val str = m_floatStrs[i]
                drawText(
                    m_native.paint,
                    str,
                    FCColor.Text,
                    font,
                    sRect.left + 5,
                    sRect.top + rowHeight * i + 3
                )
            }
        }
    }

    var m_menu: FCMenu? = null
    override fun onTouchDown(touchInfo: FCTouchInfo) {
        callTouchEvents(FCEventID.TouchDown, touchInfo)
        if (touchInfo.m_firstTouch) {
            val mp = touchInfo.m_firstPoint
            m_resizeDiv = false
            if (m_volDivPercent > 0) {
                val candleDivHeight = candleDivHeight
                val hScalePos = candleDivHeight
                if (mp.y >= hScalePos - 3 && mp.y <= hScalePos + 3) {
                    m_resizeDiv = true
                }
            }
            if (!m_resizeDiv) {
                touchDownChart(
                    touchInfo.m_firstTouch,
                    touchInfo.m_firstPoint,
                    touchInfo.m_secondTouch,
                    touchInfo.m_secondPoint,
                    touchInfo.m_clicks
                )
            }
        }
        invalidate()
    }

    var m_timerID = 0
    var m_tick = 0
    override fun onTimer(timerID: Int) {
        super.onTimer(timerID)
        if (m_tick > 0) {
            m_tick--
            if (m_tick == 0) {
                stopTimer(m_timerID)
                invalidate()
            }
        }
    }

    override fun onTouchMove(touchInfo: FCTouchInfo) {
        callTouchEvents(FCEventID.TouchMove, touchInfo)
        m_floatStrs.clear()
        stopTimer(m_timerID)
        m_tick = 0
        val mp = touchInfo.m_firstPoint
        if (m_volDivPercent > 0) {
            val candleDivHeight = candleDivHeight
            val hScalePos = candleDivHeight
            cursor = if (m_resizeDiv || mp.y >= hScalePos - 3 && mp.y <= hScalePos + 3) {
                FCCursors.SizeNS
            } else {
                FCCursors.Arrow
            }
        }
        if (m_resizeDiv) {
            val contentHeight = height - m_hScaleHeight
            if (contentHeight > 0) {
                val rate = mp.y.toDouble() / contentHeight
                if (rate >= 0.1 && rate <= 0.9) {
                    m_candleDivPercent = rate
                    m_volDivPercent = 1 - rate
                    invalidate()
                }
            }
        } else {
            touchMoveChart(
                touchInfo.m_firstTouch,
                touchInfo.m_firstPoint,
                touchInfo.m_secondTouch,
                touchInfo.m_secondPoint
            )
            if (m_datas!!.size > 0 && !m_cycle.equals("trend")) {
                val crossStopIndex = getChartIndexReal(touchInfo.m_firstPoint)
                if (crossStopIndex >= 0 && crossStopIndex <= m_lastVisibleIndex && crossStopIndex < m_datas!!.size) {
                    val securityData = m_datas!![crossStopIndex]
                    val highY = getChartY(0, securityData.m_high)
                    val lowY = getChartY(0, securityData.m_low)
                    if (mp.y >= Math.min(highY, lowY) - 5 && mp.y <= Math.max(highY, lowY) + 5) {
                        var lastClose = securityData.m_close
                        if (crossStopIndex > 0) {
                            lastClose = m_datas!![crossStopIndex - 1].m_close
                        }
                        val tFont = FCFont("Default", 12f)
                        val security = Security()
                        val strs = ArrayList<String>()
                        if (securityData.m_startDate > 0 && securityData.m_endDate > 0) {
                            val startDate = FCTran.numToDate(securityData.m_startDate)
                            val endDate = FCTran.numToDate(securityData.m_endDate)
                            val dayOfWeek1 = dayOfWeek(
                                startDate[Calendar.YEAR], startDate[Calendar.MONTH] + 1,
                                startDate[Calendar.DAY_OF_MONTH]
                            )
                            val dayOfWeek2 = dayOfWeek(
                                endDate[Calendar.YEAR], endDate[Calendar.MONTH] + 1,
                                endDate[Calendar.DAY_OF_MONTH]
                            )
                            val strWeek1 = getWeekStr(dayOfWeek1)
                            val strWeek2 = getWeekStr(dayOfWeek2)
                            val format = SimpleDateFormat(m_hScaleFormat)
                            var strDate1 = format.format(startDate.time)
                            //                            String strDate1 = startDate.toString(m_hScaleFormat);
                            strDate1 = if (m_myChart!!.m_cycle >= 1440) {
                                val forma = SimpleDateFormat("yyyy/MM/dd")
                                forma.format(startDate.time)
                                //                                strDate1 = startDate.toString("yyyy/MM/dd");
                            } else {
                                val forma = SimpleDateFormat("yyyy/MM/dd HH:mm")
                                forma.format(startDate.time)
                                //                                strDate1 = startDate.toString("yyyy/MM/dd HH:mm");
                            }
                            val forma = SimpleDateFormat(m_hScaleFormat)
                            var strDate2 = forma.format(endDate.time)
                            //                            String strDate2 = endDate.toString(m_hScaleFormat);
                            strDate2 = if (m_myChart!!.m_cycle >= 1440) {
                                val form = SimpleDateFormat("yyyy/MM/dd")
                                form.format(endDate.time)
                                //                                strDate2 = endDate.toString("yyyy/MM/dd");
                            } else {
                                val form = SimpleDateFormat("yyyy/MM/dd HH:mm")
                                form.format(endDate.time)
                                //                                strDate2 = endDate.toString("yyyy/MM/dd HH:mm");
                            }
                            if (m_myChart!!.m_cycle >= 1440) {
                                strs.add("起:$strDate1,$strWeek1")
                                strs.add("止:$strDate2,$strWeek2")
                            } else {
                                strs.add("起:$strDate1")
                                strs.add("止:$strDate2")
                            }
                        } else {
                            val dateTime = FCTran.numToDate(securityData.m_date)
                            val dayOfWeek = dayOfWeek(
                                dateTime[Calendar.YEAR], dateTime[Calendar.MONTH] + 1,
                                dateTime[Calendar.DAY_OF_MONTH]
                            )
                            val form = SimpleDateFormat(m_hScaleFormat)
                            var strDate = form.format(dateTime.time)
                            //                            String strDate = dateTime.toString(m_hScaleFormat);
                            strDate = if (m_myChart!!.m_cycle >= 1440) {
                                val formt = SimpleDateFormat("yyyy/MM/dd")
                                formt.format(dateTime.time)
                                //                                strDate = dateTime.toString("yyyy/MM/dd");
                            } else {
                                val formt = SimpleDateFormat("yyyy/MM/dd HH:mm")
                                formt.format(dateTime.time)
                                //                                strDate = dateTime.toString("yyyy/MM/dd HH:mm");
                            }
                            val strWeek = getWeekStr(dayOfWeek)
                            if (m_myChart!!.m_cycle >= 1440) {
                                strs.add("$strDate,$strWeek")
                            } else {
                                strs.add(strDate)
                            }
                        }
                        strs.add("开:" + FCTran.getValueByDigit(securityData.m_open, 2))
                        strs.add("高:" + FCTran.getValueByDigit(securityData.m_high, 2))
                        strs.add("低:" + FCTran.getValueByDigit(securityData.m_low, 2))
                        strs.add("收:" + FCTran.getValueByDigit(securityData.m_close, 2))
                        strs.add(
                            "量:" + FCTran.getValueByDigit(
                                securityData.m_volume / 100,
                                0
                            ) + "手"
                        )
                        strs.add(
                            "额:" + FCTran.getValueByDigit(
                                securityData.m_amount / 10000,
                                0
                            ) + "万"
                        )
                        var rate = 0.0
                        if (lastClose != 0.0) {
                            rate = 100 * (securityData.m_close - lastClose) / lastClose
                        }
                        strs.add("涨:" + FCTran.getValueByDigit(rate, 2) + "%")
                        var range = 0.0
                        if (lastClose != 0.0) {
                            range =
                                100 * (securityData.m_high - lastClose) / lastClose - 100 * (securityData.m_low - lastClose) / lastClose
                        }
                        strs.add("振:" + FCTran.getValueByDigit(range, 2) + "%")
                        m_floatStrs = strs
                        m_tick = 3
                        startTimer(m_timerID, 100)
                    }
                }
            }
        }
        invalidate()
    }

    override fun onTouchUp(touchInfo: FCTouchInfo) {
        if (m_resizeDiv) {
            m_resizeDiv = false
        }
    }

    var m_resizeDiv = false
    var m_myChart: MyChart? = null
    var m_subComboBox: FCComboBox? = null

    /*
     * 添加视图
     */
    override fun onAdd() {
        super.onAdd()
        if (m_subComboBox == null) {
            m_subComboBox = FCComboBox()
            addView(m_subComboBox)
            m_subComboBox!!.isVisible = false
            m_subComboBox!!.isReadOnly = true
            m_subComboBox!!.font = FCFont("Default", 12f)
            m_subComboBox!!.lineHeight = 10
            m_subComboBox!!.borderColor = FCColor.None
            m_subComboBox!!.text = "不复权"
            m_subComboBox!!.textColor = FCColor.Text
            val subStrs = ArrayList<String>()
            subStrs.add("前复权")
            subStrs.add("后复权")
            subStrs.add("不复权")
            for (i in subStrs.indices) {
                val menuItem = FCMenuItem()
                menuItem.text = subStrs[i]
                menuItem.borderColor = FCColor.None
                m_subComboBox!!.addItem(menuItem)
            }
            m_subComboBox!!.update()
            m_subComboBox!!.selectedIndex = 0
            m_subComboBox!!.dropDownButton.borderColor = FCColor.None
            m_subComboBox!!.dropDownMenu.minimumSize = FCSize(80, 60)
            m_subComboBox!!.addEvent(this, "onselectedindexchanged", this)
        }
    }

    var m_keyDownTick = 0
    override fun onKeyUp(key: Char) {
        super.onKeyUp(key)
        m_keyDownTick = 0
    }

    val subScription: Int
        get() {
            val sIndex = m_subComboBox!!.selectedIndex
            return if (sIndex == 0) {
                1
            } else if (sIndex == 1) {
                2
            } else {
                0
            }
        }

    override fun callEvent(eventName: String, sender: Any, invoke: Any) {
        if (m_myChart!!.m_cycle > 0) {
            m_myChart!!.m_lastPixel = m_hScalePixel
        }
        m_myChart!!.searchSecurity(m_myChart!!.m_code, m_myChart!!.m_cycle)
    }

    companion object {
        fun getWeekStr(week: Int): String {
            return when (week) {
                0 -> "一"
                1 -> "二"
                2 -> "三"
                3 -> "四"
                4 -> "五"
                5 -> "六"
                else -> "日"
            }
        }
    }
}

